<!DOCTYPE html>
<!-- This is not a WPT test because it tests non-specified Chrome-specific behavior. It tests that self-updating service workers eventually fail to update. -->
<title>Service Worker: update no controllee</title>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="resources/test-helpers.js"></script>
<body>
<script>
promise_test(async (t) => {
  // Set up ServiceWorker and wait until it's |activated|.
  const url = 'resources/update-no-controllee-worker.js';
  const scope = 'resources/blank.html';
  const registration = await service_worker_unregister_and_register(t, url, scope);
  await wait_for_state(t, registration.installing, 'activated');

  const frame = await with_iframe(scope);

  const w = frame.contentWindow;
  const sw = w.navigator.serviceWorker;
  assert_true(sw.controller instanceof w.ServiceWorker,
              'controller should be a ServiceWorker object');

  const channel = new MessageChannel();
  sw.controller.postMessage(channel.port1, [channel.port1]);

  // Remove frame so our worker has no controller.
  frame.remove();
  assert_equals(
      sw.controller, null, 'disconnected frame should not be controlled');

  // Send a message to the worker to start updating and wait for its response.
  // We expect at least one of the updates, and therefore the complete proccess,
  // to fail immediately because the delay is too long.
  const result = wait_for_port_message(channel.port2, (e) => {
    assert_equals(e.data, 'failure', 'update should not have succeeded');
  });
  channel.port2.postMessage('');
  await result;

  // Cleanup.
  await registration.unregister();
}, 'Verify multiple updates from service workers without controllees ' +
       'do not resolve immediately');
</script>
</body>
